﻿/*
    Project: IV:MP GUI Editor (http://code.google.com/p/ivmp-gui-editor)
    Code license: GNU Lesser GPL (http://www.gnu.org/copyleft/lesser.html) 
 */

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace IvmpEditor
{
    public class GUICheckBox : GUIElement
    {
        public new const string TYPE = "GUICheckBox";
        public override string GetTypeName() { return TYPE; }
        public override string GetDefaultVarName() { return "checkBox"; }
        private new CheckBox nativeControl;
        public new CheckBox NativeControl { get { return this.nativeControl; } }

        bool checked_ = false;
        public bool CheckOnClick = false;
        void nativeControl_CheckedChanged(object sender, RoutedEventArgs e)
        {
            if(!CheckOnClick && nativeControl.IsChecked != checked_)
                nativeControl.IsChecked = checked_;
        }
        public override void SetText(string newTxt, bool native)
        {
            base.SetText(newTxt, native);
            if (native && nativeControl != null)
                nativeControl.Content = newTxt;
        }
        public void SetChecked(bool newCheckedValue, bool native)
        {
            checked_ = newCheckedValue;
            PropertySet("Checked", newCheckedValue);
            if (native && nativeControl != null)
                nativeControl.IsChecked = newCheckedValue;
        }
        public void SetChecked(bool newCheckedValue) { SetChecked(newCheckedValue, nativeControl != null); }
        public bool IsChecked
        {
            get { return (bool)PropertyGet("Checked", false); }
            set { SetChecked(value); }
        }

        public GUICheckBox(string _variableName)
        {
            variableName = _variableName;
            properties = new Dictionary<string, object>(25);
            MakeControl();

            SetChecked(false, true);
        }
        protected override void MakeControl()
        {
            // Create a native control stylized with CEGUI
            nativeControl = new CheckBox();
            base.nativeControl = this.nativeControl;
            moveControlAgent = new WpfMoveControlAgent(nativeControl);

            nativeControl.Style = GetBaseStyle();
            SetControlFont(nativeControl, "#tahoma-bold", 12, FontWeights.Black);
            nativeControl.Content = variableName;

            nativeControl.Checked += nativeControl_CheckedChanged;
            nativeControl.Unchecked += nativeControl_CheckedChanged;
        }
        public override void SyncFromNative()
        {
            base.SyncFromNative();
            PropertySet("Checked", nativeControl.IsChecked);            
        }
        public override void SyncToNative()
        {
            base.SyncToNative();
            nativeControl.IsChecked = (bool)PropertyGet("Checked");
        }
        public override string GetSQCode(ref SquirrelCodeStack sq)
        {
            sq.wSet(null, variableName, TYPE + "()", true);

            WriteElementCommonCode(ref sq, new Point(2.6, -2.1), variableName);

            foreach (var e in properties)
            {
                switch (e.Key)
                {
                        // TODO clean
                    case "Checked":
                        bool chk = (bool)e.Value;
                        sq.wFunc(null, variableName, "setChecked", chk.ToString().ToLower());
                        break;
                }
            }

            return variableName;
        }
        public override Style GetBaseStyle()
        {
            return App.Current.FindResource("CEGUI_CheckBox") as Style;
        }
    }
}
